package com.service;

import com.bean.base.DictInfo;
import com.bean.base.DictItemInfo;
import com.bean.other.MetaInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.DictMapper;
import com.mapper.repository.DictInfoRepository;
import com.mapper.repository.DictItemInfoRepository;
import com.service.impl.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private DictMapper dictMapper;

    @Autowired
    private DictInfoRepository dictInfoRepository;

    @Autowired
    private DictItemInfoRepository dictItemInfoRepository;

    @Override
    public String insertDict(DictInfo dictInfo) {
        //判断value是否已经存在
        if(dictInfo.getValue()!=null){
            int count=dictMapper.countByDictValue(dictInfo.getValue());
            if(count>0){
                return "该字典值已存在";
            }else{
                dictInfo.setId(UUID.randomUUID().toString());
                dictInfo.setCreatetime(LocalDateTime.now());
                dictInfoRepository.save(dictInfo);
                return "新增成功";
            }
        }else{
            return "字典值为必填项";
        }
    }

    @Override
    public String updateDict(DictInfo dictInfo) {
        dictInfo.setUpdatetime(LocalDateTime.now());
        dictInfoRepository.save(dictInfo);
        return "更新成功";
    }

    @Override
    public String deleteDict(DictInfo dictInfo) {
        dictInfoRepository.delete(dictInfo);
        return "删除成功";
    }

    @Override
    public PageInfo<DictInfo> selectDict(DictInfo dictInfo, MetaInfo metaInfo) {
        PageHelper.startPage(metaInfo.getPage(), metaInfo.getSize(),"createtime");
        List<DictInfo> list=dictMapper.selectDict(dictInfo);
        PageInfo<DictInfo> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public String insertDictItem(DictItemInfo dictItemInfo) {
        if(dictItemInfo.getDictId()!=null&&dictItemInfo.getValue()!=null&&!dictItemInfo.getDictId().equals("")){
            int count=dictMapper.countByDictItenValueAndDictId(dictItemInfo.getValue(),dictItemInfo.getDictId());
            if(count>0){
                return "字典项值已存在";
            }else{
                dictItemInfo.setId(UUID.randomUUID().toString());
                dictItemInfo.setCreatetime(LocalDateTime.now());
                dictItemInfoRepository.save(dictItemInfo);
                return "新增成功";
            }
        }else{
            return "字典ID和字典项值为必填项";
        }
    }

    @Override
    public String updateDictItem(DictItemInfo dictItemInfo) {
        dictItemInfo.setUpdatetime(LocalDateTime.now());
        dictItemInfoRepository.save(dictItemInfo);
        return "更新成功";
    }

    @Override
    public String deleteDictItem(DictItemInfo dictItemInfo) {
        dictItemInfoRepository.delete(dictItemInfo);
        return "删除成功";
    }

    @Override
    public PageInfo<DictItemInfo> selectDictItem(DictItemInfo dictItemInfo, MetaInfo metaInfo) {
        PageHelper.startPage(metaInfo.getPage(), metaInfo.getSize(),"createtime");
        List<DictItemInfo> list=dictMapper.selectDictItem(dictItemInfo);
        PageInfo<DictItemInfo> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<DictItemInfo> selectDictItem(DictItemInfo dictItemInfo) {
        if(dictItemInfo.getValue()!=null){
            List<DictItemInfo> list=dictMapper.selectItem(dictItemInfo);
            return list;
        }
        return null;
    }
}
